package com.asia5b.a5mall.order.adapter

import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.graphics.Typeface
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5mall.middleware.model.order.ChargeBean
import com.asia5b.a5mall.middleware.model.bean.DiffOrderMenuBean

import com.asia5b.a5mall.middleware.utils.CurrencyPriceBuilder3
import com.asia5b.a5mall.middleware.utils.showMessage
import com.asia5b.a5mall.order.R
import com.asia5b.a5mall.order.databinding.ItemOrderplacePriceBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * Created by zhangyuncai on 2020/1/9.
 */
class DiffOrderPriceAdapter(list: ArrayList<DiffOrderMenuBean>) :
        BaseQuickAdapter<DiffOrderMenuBean, BaseViewHolder>(R.layout.item_difforder_price, list) {
    override fun convert(helper: BaseViewHolder, item: DiffOrderMenuBean) {
        //名称
        val tv_orderplace_price_name = helper.getView<TextView>(R.id.tv_orderplace_price_name)
        tv_orderplace_price_name.text = item.type
        if (TextUtils.equals("综合费用", item.typetag)) {
            tv_orderplace_price_name.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗
        }
        //值
        val tv_orderplace_price_value = helper.getView<TextView>(R.id.tv_orderplace_price_value)
        val tv_orderplace_price_expand = helper.getView<TextView>(R.id.tv_orderplace_price_expand)
        val rv_orderplace_option = helper.getView<RecyclerView>(R.id.rv_orderplace_option)
        if (item.value is String) {
            tv_orderplace_price_value.text = "${item.value}"

            if (TextUtils.equals("订单编号", item.typetag)) {
                tv_orderplace_price_expand.visibility = View.VISIBLE
                tv_orderplace_price_expand.text = ResString.get(R.string.复制)
                tv_orderplace_price_expand.setOnClickListener {
                    if (context is Activity) {
                        val clipboardManager = (context as Activity).getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                        clipboardManager.setPrimaryClip(ClipData.newPlainText(null, "${item.value}"))
                        showMessage(R.string.复制成功)
                    }
                }
            }
        } else if (item.value is ChargeBean) {
            if (TextUtils.equals("综合费用", item.typetag)) {
                tv_orderplace_price_expand.visibility = View.GONE

                val bean = item.value as ChargeBean
                CurrencyPriceBuilder3().run {
                    textView = tv_orderplace_price_value
                    price = bean.charge
                    priceList = bean.charge_list
                    build()
                }
            } else {
                tv_orderplace_price_expand.visibility = View.VISIBLE

                val bean = item.value as ChargeBean
                CurrencyPriceBuilder3().run {
                    textView = tv_orderplace_price_value
                    price = bean.charge
                    priceList = bean.charge_list
                    build()
                }
                item.valueOption?.run {
                    val optionAdapter = OptionAdapter(this, item)
                    rv_orderplace_option.run {
                        layoutManager = LinearLayoutManager(context)
                        adapter = optionAdapter
                    }
                }

                tv_orderplace_price_expand.setOnClickListener {
                    if (rv_orderplace_option.visibility == View.GONE) {
                        rv_orderplace_option.visibility = View.VISIBLE
                    } else {
                        rv_orderplace_option.visibility = View.GONE
                    }
                }
            }
        }


        //字体颜色
        when (item.typetag) {
            "打包费用",
            "木架费用",
            "国内运费",
            "国际运费"->{//价格显示红色
                tv_orderplace_price_value.setTextColor(ContextCompat.getColor(context,R.color.color_red))
            }
            else->{
                tv_orderplace_price_value.setTextColor(ContextCompat.getColor(context,R.color.common_text_black))
            }
        }

    }
}

class OptionAdapter(list: ArrayList<ChargeBean>, diffOrderMenuBean: DiffOrderMenuBean) :
        BaseQuickAdapter<ChargeBean, BaseDataBindingHolder<ItemOrderplacePriceBinding>>(R.layout.item_orderplace_price, list) {
    override fun convert(helper: BaseDataBindingHolder<ItemOrderplacePriceBinding>, item: ChargeBean) {
        //名称
        val tv_orderplace_price_name = helper.getView<TextView>(R.id.tv_orderplace_price_name)
        tv_orderplace_price_name.text = item.main_order_code
        //值
        val tv_orderplace_price_value = helper.getView<TextView>(R.id.tv_orderplace_price_value)
        CurrencyPriceBuilder3().run {
            textView = tv_orderplace_price_value
            price = item.charge
            priceList = item.charge_list
            build()
        }

    }
}